package fr.fermeManager.application.views.components;

import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;

import fr.fermeManager.application.utils.AppContext;
import fr.fermeManager.application.utils.Constantes;
import fr.fermeManager.library.model.beans.TypeTransaction;
import fr.fermeManager.library.model.beans.UserFermeManager;

public class ComponentTypeTransaction extends JPanel {

	private boolean isAdmin;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private BindingGroup m_bindingGroup;
	private TypeTransaction typeTransaction = new TypeTransaction(true);
	private JCheckBox actifJCheckBox;
	private JTextField descriptionJTextField;
	private JTextField idJTextField;
	private JTextField idParentJTextField;
	private JTextField libelleJTextField;
	private JLabel lblPossdeDes;

	/**
	 * @wbp.parser.constructor
	 */
	public ComponentTypeTransaction(TypeTransaction newTypeTransaction, boolean isAdmin,boolean possedeElementsActifs) {
		this(isAdmin,possedeElementsActifs);
		setTypeTransaction(newTypeTransaction, true);
	}

	public ComponentTypeTransaction(boolean isAdminParam,boolean possedeElementsActifs) {

		isAdmin = isAdminParam;

		setBorder(new TitledBorder(null, "Edition Type Transaction", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setMinimumSize(new Dimension(390, 240));
		setLayout(new MigLayout("", "[57px,grow][270px,grow]", "[21px,grow][20px,grow][20px,grow][20px,grow][20px,grow]"));

		if(isAdmin){
			JLabel actifLabel = new JLabel("Actif:");
			add(actifLabel, "cell 0 0,alignx center,aligny center");

			actifJCheckBox = new JCheckBox();
			actifJCheckBox.setEnabled(!possedeElementsActifs);
			add(actifJCheckBox, "flowx,cell 1 0,growx,aligny center");
		}

		JLabel idLabel = new JLabel("Id:");
		add(idLabel, "cell 0 1,alignx center,aligny center");

		idJTextField = new JTextField();
		idJTextField.setEditable(false);
		add(idJTextField, "cell 1 1,growx,aligny center");

		JLabel idParentLabel = new JLabel("IdParent:");
		add(idParentLabel, "cell 0 2,alignx center,aligny center");

		idParentJTextField = new JTextField();
		idParentJTextField.setEditable(false);
		add(idParentJTextField, "cell 1 2,growx,aligny center");

		JLabel libelleLabel = new JLabel("Libelle:");
		add(libelleLabel, "cell 0 3,alignx center,aligny center");

		libelleJTextField = new JTextField();
		libelleJTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				libelleJTextField.selectAll();
			}
		});
		add(libelleJTextField, "cell 1 3,growx,aligny center");

		JLabel descriptionLabel = new JLabel("Description:");
		add(descriptionLabel, "cell 0 4,alignx center,aligny center");

		descriptionJTextField = new JTextField();
		descriptionJTextField.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				descriptionJTextField.selectAll();
			}
		});
		add(descriptionJTextField, "cell 1 4,growx,aligny center");

		if(isAdmin && possedeElementsActifs){
			lblPossdeDes = new JLabel("( Poss\u00E8de des \u00E9l\u00E9ments Actifs )");
			add(lblPossdeDes, "cell 1 0");
		}

		if (typeTransaction != null) {
			m_bindingGroup = initDataBindings();
		}
		setVisible(true);
		SwingUtilities.invokeLater(new Runnable()
		{
		   @Override
		   public void run()
		   {
			   libelleJTextField.requestFocus();	
		   }
		 
		});
	}

	public TypeTransaction getTypeTransaction() {
		return typeTransaction;
	}

	public void setTypeTransaction(TypeTransaction newTypeTransaction) {
		UserFermeManager currentUser = (UserFermeManager) AppContext.getSession().get(Constantes.USER_FERME_MANAGER);
		actifJCheckBox.setEnabled(false);
		if (currentUser.isAdmin()) {
			actifJCheckBox.setEnabled(true);
		}
		setTypeTransaction(newTypeTransaction, true);
	}

	public void setTypeTransaction(TypeTransaction newTypeTransaction, boolean update) {
		typeTransaction = newTypeTransaction;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (typeTransaction != null) {
				m_bindingGroup = initDataBindings();
			}
		}
	}

	public void sauvegarder() {
		for (@SuppressWarnings("rawtypes")
		Binding bindingTmp : m_bindingGroup.getBindings()) {
			bindingTmp.unbind();
			bindingTmp.save();
			bindingTmp.bind();
		}
	}

	public void recharger() {
		for (@SuppressWarnings("rawtypes")
		Binding bindingTmp : m_bindingGroup.getBindings()) {
			bindingTmp.unbind();
			bindingTmp.refresh();
			bindingTmp.bind();
		}
	}

	protected BindingGroup initDataBindings() {
		BeanProperty<TypeTransaction, Boolean> actifProperty = BeanProperty.create("actif");
		BeanProperty<JCheckBox, Boolean> selectedProperty = BeanProperty.create("selected");
		AutoBinding<TypeTransaction, Boolean, JCheckBox, Boolean> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction,
				actifProperty, actifJCheckBox, selectedProperty);
		autoBinding.bind();
		//
		BeanProperty<TypeTransaction, String> descriptionProperty = BeanProperty.create("description");
		BeanProperty<JTextField, String> textProperty = BeanProperty.create("text");
		AutoBinding<TypeTransaction, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction,
				descriptionProperty, descriptionJTextField, textProperty);
		autoBinding_1.bind();
		//
		BeanProperty<TypeTransaction, Integer> idProperty = BeanProperty.create("id");
		BeanProperty<JTextField, String> textProperty_1 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, Integer, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction,
				idProperty, idJTextField, textProperty_1);
		autoBinding_2.bind();
		//
		BeanProperty<TypeTransaction, Integer> idParentProperty = BeanProperty.create("idParent");
		BeanProperty<JTextField, String> textProperty_2 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, Integer, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction,
				idParentProperty, idParentJTextField, textProperty_2);
		autoBinding_3.bind();
		//
		BeanProperty<TypeTransaction, String> libelleProperty = BeanProperty.create("libelle");
		BeanProperty<JTextField, String> textProperty_3 = BeanProperty.create("text");
		AutoBinding<TypeTransaction, String, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, typeTransaction,
				libelleProperty, libelleJTextField, textProperty_3);
		autoBinding_4.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		//
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_1);
		bindingGroup.addBinding(autoBinding_2);
		bindingGroup.addBinding(autoBinding_3);
		bindingGroup.addBinding(autoBinding_4);
		return bindingGroup;
	}

	public String getDescription() {
		return this.descriptionJTextField.getText();
	}

	public String getLibelle() {
		return this.libelleJTextField.getText();
	}
}
